package com.hh.org.controller;

import com.hh.basic.util.AjaxResult;
import com.hh.basic.util.PageList;
import com.hh.org.domain.Emp;
import com.hh.org.query.EmpQuery;
import com.hh.org.service.IEmpService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 黄浩
 * @create 2022-06-03 17:07
 */
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private IEmpService empService;

    /**
     * 返回分解＋高级查询的结果集
     * @param empQuery
     * @return
     */
    @PostMapping
    public PageList<Emp> loadAll(@RequestBody EmpQuery empQuery){
        return empService.queryPage(empQuery);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Emp> queryAll(){
        return empService.list();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Emp queryById(@PathVariable("id") Long id){
        return empService.getById(id);
    }

    /**
     * 执行新增或修改
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Emp emp){
        try {
            empService.saveOrUpdate(emp);
            return AjaxResult.getInstance().setSuccess(true).setMessage("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getInstance().setSuccess(false).setMessage("出了点状况。。");
        }
    }

    /**
     * 根据id删除
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            empService.removeById(id);
            return AjaxResult.getInstance().setSuccess(true).setMessage("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getInstance().setSuccess(false).setMessage("出了点状况。。");
        }
    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            empService.removeByIds(ids);
            return AjaxResult.getInstance().setSuccess(true).setMessage("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getInstance().setSuccess(false).setMessage("出了点状况。。");
        }
    }
}
